//
//  DwtHear.m
//  生活小助手
//
//  Created by dwt on 14-8-9.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "DwtHeaderView.h"
#import "DwtFootCategoryParent.h"
@interface DwtHeaderView ()

@end

@implementation DwtHeaderView
// 创建头部视图
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    //    1.创建头部视图
    static NSString *identifier = @"header";
    DwtHeaderView *headderView =  [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (headderView == nil) {
        headderView = [[DwtHeaderView alloc] initWithReuseIdentifier:identifier];
    }
    return headderView;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        //        1.添加子控件
        // 1.1添加按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        // 监听按钮的点击事件
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        // 设置按钮的背景图片
        
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [self addSubview:btn];
        self.btn = btn;
        
    }
    
    return  self;
}

-(void)setFootCategoryParent:(DwtFootCategoryParent *)footCategoryParent{
    _footCategoryParent=footCategoryParent;
    [self.btn setTitle:footCategoryParent.name forState:UIControlStateNormal];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //    1.设置按钮的frame
    self.btn.frame = self.bounds;
}
- (void)btnOnClick:(UIButton *)btn{

        //     1.修改组模型的isOpen属性
        //    修改模型数据数据
        self.footCategoryParent.open = !self.footCategoryParent.isOpen;
        //    2. 刷新表格(通知代理)
        if ([self.delegate respondsToSelector:@selector(headerViewDidClickHeaderView:)]) {
            [self.delegate headerViewDidClickHeaderView:self];
        }
}

@end
